package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.AdminBank;
import com.bjsxt.dto.AdminBankDto;
import com.bjsxt.feign.AdminBankFeignClient;
import com.bjsxt.model.Result;
import com.bjsxt.service.AdminBankService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 银行卡的配置控制层
 * 银行卡的配置的权限:
 * 查询： admin_bank_query
 * 修改： admin_bank_update
 * 新增： admin_bank_create
 * 删除： admin_bank_delete
 *
 * @author ALIENWARE <br />
 * @date 2021/7/25
 */
@RestController
@RequestMapping("/adminBanks")
@Api(tags = "银行卡的配置控制器")
public class AdminBankController implements AdminBankFeignClient {
    /**
     * 银行卡的配置业务层接口
     */
    @Autowired
    private AdminBankService adminBankService;

    /**
     * 分页查询银行卡数据
     *
     * @param page     分页对象<AdminBank>
     * @param bankCard 银行卡卡号
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('admin_bank_query')")
    @ApiOperation(value = "分页查询银行卡数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "bankCard", value = "银行卡卡号", dataType = "String", paramType = "query", example = "10012455236551")
    })
    public Result<Page<AdminBank>> findByPage(@ApiIgnore Page<AdminBank> page, String bankCard) {
        // 调用业务层进行分页数据的查询
        Page<AdminBank> adminBankPage = adminBankService.pageByBankCard(page, bankCard);
        // 返回响应数据
        return Result.success(adminBankPage);
    }

    /**
     * 根据银行卡 ID 获取银行卡数据
     *
     * @param id 银行卡 ID
     * @return 统一响应对象
     */
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('admin_bank_query')")
    @ApiOperation(value = "获取银行卡详细信息", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "银行卡 ID", dataType = "Integer", required = true, paramType = "query", example = "1")
    )
    public Result<AdminBank> getById(@NotNull Long id) {
        // 根据银行卡 ID 获取银行卡详情数据
        AdminBank adminBank = adminBankService.getById(id);
        // 返回响应数据
        return Result.success(adminBank);
    }

    /**
     * 获取所有的银行卡
     *
     * @return 银行卡数据传输对象集合
     */
    @Override
    @ApiOperation(value = "获取所有的银行卡信息", authorizations = {@Authorization("Authorization")})
    public List<AdminBankDto> getAdminBanks() {
        // 调用业务层进行数据的查询并返回 dto 对象
        return adminBankService.listByStatus((byte) 1);
    }

    /**
     * 增加一个银行卡数据
     *
     * @param adminBank 人民币充值卡号管理对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('admin_bank_create')")
    @ApiOperation(value = "增加一个银行卡数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "AdminBank 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@RequestBody @Validated AdminBank adminBank) {
        // 调用业务层方法进行数据的保存
        boolean flag = adminBankService.save(adminBank);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 修改一个银行卡数据
     *
     * @param adminBank 人民币充值卡号管理对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('admin_bank_update')")
    @ApiOperation(value = "修改一个银行卡数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "AdminBank 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> update(@RequestBody @Validated AdminBank adminBank) {
        // 调用业务层方法进行数据的保存
        boolean flag = adminBankService.updateById(adminBank);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 启用/禁用一张银行卡
     *
     * @param id     要启用/禁用银行卡的 Id
     * @param status 要设置的公告状态值 1: 启用 0: 禁用
     * @return 统一响应对象
     */
    @PostMapping("/adminUpdateBankStatus")
    @PreAuthorize("hasAuthority('admin_bank_update')")
    @ApiOperation(value = "启用/禁用一张银行卡", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "要启用/禁用银行卡的 Id", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "status", value = "要设置的公告状态值 1: 启用 0: 禁用", dataType = "Byte", paramType = "query", example = "0")
    })
    public Result<Void> updateStatus(@RequestParam("bankId") @NotNull Long id, Byte status) {
        // 创建更新的 notice 对象
        AdminBank adminBank = new AdminBank();
        // 设置 id 及需要更新的值
        adminBank.setId(id);
        adminBank.setStatus(status);
        // 调用业务层方法进行数据的保存
        boolean flag = adminBankService.updateById(adminBank);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }
}
